package com.ccs.demo;

import jakarta.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.codec.binary.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

@Controller
@RequestMapping("/return")
public class ReturnControllerNew {

    @RequestMapping("/returnPage")
    public String returnPage(){
        return "/index.html";
    }

    @ResponseBody
    @RequestMapping("/returnData")
    public String returnData(){
        return "/index.html";
    }

    @ResponseBody
    @RequestMapping("/returnHtml")
    public String returnHtml(){
        return "<h1>Html</h1>";
    }

    @ResponseBody
    @RequestMapping("/returnJson1")
    public HashMap<String,String> returnJson1(){
        HashMap<String,String> map=new HashMap<>();
        map.put("java","javaValue");
        map.put("c++","c++Value");
        map.put("mysql","mysqlValue");
        return map;
    }

    @ResponseBody
    @RequestMapping("/returnJson2")
    public Person returnJson2(){
        Person person=new Person();
        person.setName("ccs");
        person.setAge(19);
        return person;
    }

    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response){
        response.setStatus(401);
        return "设置状态码成功";
    }

    @ResponseBody
    @RequestMapping("/setHeader")
    public String setHeader(HttpServletResponse response){
        response.setHeader("My-Header","MyHeader");
        return "设置Header成功";
    }

    @ResponseBody
    @RequestMapping("/setContentType")
    public void setContentType(HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        response.getOutputStream().write("数据".getBytes(StandardCharsets.UTF_8));
    }

    @ResponseBody
    @RequestMapping(value = "setContentType2",produces = "text/html")
    public String setContentType2(){
        return "数据";
    }

}
